"""
模块-: 数据库建模
@Author          :王海波
@Version         ：V 1.0.0
@CreateTime       ：2020/10/20
-----------------------------------
@ModifyTime       : 2020/10/20
@ModifyContent    : 2020/10/20
@Remark           : 备注


"""
# 导包
from bluepro import db

# 创建baseModel类，继承db.Model，本身类不会执行
class BaseModel(db.Model):
    __abstract__ = True
    id = db.Column(db.Integer, primary_key=True,autoincrement=True)

    # 创建保存方法
    def save(self):
        session = db.session()
        session.add(self)    # add_all([p1,p2,p3,p4])  添加多条
        session.commit()
    # 创建更新方法
    def update(self):
        session = db.session()
        session.commit()
    # 创建删除方法
    def delete(self):
        session = db.session()
        session.delete(self)
        session.commit()

#职位与权限 多对多
position_per = db.Table(
    "position_per",
    db.Column("position_id",db.Integer,db.ForeignKey("position.id")),
    db.Column("permission_id", db.Integer, db.ForeignKey("permission.id")),
)

#员工与权限 多对多
person_per = db.Table(
    "person_per",
    db.Column("person_id", db.Integer, db.ForeignKey("person.id")),
    db.Column("permission_id", db.Integer, db.ForeignKey("permission.id")),
)



#新闻
class News(BaseModel):
    __tablename__ = "news"
    title = db.Column(db.String(128))    # 标题
    public_time = db.Column(db.Date)    # 时间
    author = db.Column(db.String(62))    # 作者
    content = db.Column(db.Text)    # 内容
    picture = db.Column(db.String(256))   # 图片

# 部门
class Department(BaseModel):
    __tablename__ = "department"
    d_name = db.Column(db.String(32))
    d_dec = db.Column(db.Text)

    # 职员与部门 多对一 反向映射
    d_person = db.relationship(
        "PersonInfo",
        backref = "per_department" #职员查询部门
    )

    ##职位和部门 多对一 反向映射
    d_position = db.relationship(
        "Position",
        backref = "position_department" # 职位查询部门的
    )

#职位
class Position(BaseModel):
    __tablename__ = "position"
    p_name = db.Column(db.String(32))
    p_dec = db.Column(db.Text)

    #职位和部门 多对一
    pos_department = db.Column(db.Integer,db.ForeignKey("department.id"))

    #职员与职位 多对一 反向映射
    pos_person = db.relationship(
        "PersonInfo",
        backref = "per_position" #职员查询职位
    )

    #职位和权限 多对多 反向映射
    position_permission = db.relationship(
        "Permission",
        secondary = position_per,
        backref = "permission_position" #权限查询职位
    )

#员工
class Person(BaseModel):
    __tablename__ = "person"
    username = db.Column(db.String(32),unique = True)  # 昵称
    password = db.Column(db.String(32))   # 密码

    # 职员与职员详情 一对一 反向映射
    p_info = db.relationship(
        "PersonInfo",
        backref = "person_id", #用户详情查询用户
        uselist = False
    )

    # 绩效和职员是 多对一 反向映射
    p_kpi = db.relationship(
        "Kpi",
        backref = "k_p" #kpi查询职员
    )

    # 考勤与员工  多对一 反向映射
    p_attendance = db.relationship(
        "Attendance",
        backref="attendance_person"  # 考勤查询职员
    )
    #员工与权限 多对多 反向映射
    person_permission = db.relationship(
        "Permission",
        secondary = person_per,
        backref = "permission_person" #权限查询员工
    )

#用户详情
class PersonInfo(BaseModel):
    __tablename__ = "personInfo"
    nick_name = db.Column(db.String(32)) #名字
    gender = db.Column(db.String(32))  # 性别
    age = db.Column(db.Integer)  # 年龄
    work_number = db.Column(db.String(32),unique = True) #工号
    phone = db.Column(db.String(32)) #电话
    email = db.Column(db.String(128)) #邮箱
    photo = db.Column(db.String(128)) #照片
    address = db.Column(db.Text) #地址
    #职员与职员详情 一对一
    person_info = db.Column(db.Integer,db.ForeignKey("person.id"))
    #职员与职位 多对一
    p_positon = db.Column(db.Integer,db.ForeignKey("position.id"))
    #职员与部门 多对一
    p_department = db.Column(db.Integer, db.ForeignKey("department.id"))

#绩效
class Kpi(BaseModel):
    __tablename__ = "kpi"
    month = db.Column(db.Integer)
    value = db.Column(db.Float)
    k_dec = db.Column(db.Text)

    #绩效和职员是 多对一
    kpi_person = db.Column(db.Integer,db.ForeignKey("person.id"))


#考勤（假条）
class Attendance(BaseModel):
    __tablename__ = "attendance"
    cause = db.Column(db.Text)
    a_type = db.Column(db.String(32))
    v_length = db.Column(db.Float)
    start_time = db.Column(db.DateTime)
    end_time = db.Column(db.DateTime)
    status = db.Column(db.Integer) #考勤状态 0 申请中  1撤回   2通过   3驳回   4销假
    reviewer = db.Column(db.Integer)
    # 考勤与员工  多对一
    a_person = db.Column(db.Integer,db.ForeignKey("person.id"))


#权限
class Permission(BaseModel):
    __tablename__ = "permission"
    per_name = db.Column(db.String(32))
    per_dec = db.Column(db.Text)